/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean.TX.tta;

import helper.tx.tta.Helper_CustomerLoyalty;
import helper.tx.tta.Helper_CustomerValue;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import session.TX.CustomerAnalysisSBRemote;

/**
 *
 * @author Lu Xiaodi
 */
@ManagedBean(name = "custAnalysisMB")
@ViewScoped
public class OutletCustAnalysisMB {

    @EJB
    CustomerAnalysisSBRemote analysisBean;
    private Double discountRate = new Double(0.15);
    private Double expPerRes = new Double(100);
    private Double avgExp_CustValue = new Double(100);
    private NumberFormat formatter;

    /** Creates a new instance of OutletCustAnalysisMB */
    public OutletCustAnalysisMB() {
        formatter = new DecimalFormat("#0.00");
    }
    
    public String getCLVStr(){ 
        Double clv = this.getCLV();
        return formatter.format(clv);
    }

    public Double getCLV() {
        String stakeholderName = getStakeholderName();
        if (stakeholderName == null) {
            return null;
        } else {
            if (discountRate != null && expPerRes != null) {
                return analysisBean.calculateGC(stakeholderName, discountRate, expPerRes);
            } else {
                return null;
            }
        }
    }
    
    public void updateCLVParameter(ActionEvent event){
    }
    
    public List<Helper_CustomerLoyalty> getSortedLocalLoyaltyIdx() {
        Long outletId = getOutletId();
        if (outletId == null) {
            return null;
        }
        return analysisBean.getSortedLocalLoyaltyIdx(outletId);
    }

    public List<Helper_CustomerLoyalty> getSortedForeignLoyaltyIdx() {
        Long outletId = getOutletId();
        if (outletId == null) {
            return null;
        }
        return analysisBean.getSortedForeignLoyaltyIdx(outletId);
    }
    
    public List<Helper_CustomerValue> getSortedLocalCustValue(){
        Long outletId = getOutletId();
        if (outletId == null) {
            return null;
        }
        List<Helper_CustomerValue> returnList = analysisBean.getSortedLocalCustValue(outletId, avgExp_CustValue);
        for(int i = 0 ; i < returnList.size(); i++) {
            returnList.get(i).setRank(i+1);
        }
        return returnList;
    }
    
    public List<Helper_CustomerValue> getSortedForeignCustValue(){
        Long outletId = getOutletId();
        if (outletId == null) {
            return null;
        }
        List<Helper_CustomerValue> returnList = analysisBean.getSortedForeignCustValue(outletId, avgExp_CustValue);
        for(int i = 0 ; i < returnList.size(); i++) {
            returnList.get(i).setRank(i+1);
        }
        return returnList;
    }

    public Double getDiscountRate() {
        return discountRate;
    }

    public void setDiscountRate(Double discountRate) {
        this.discountRate = discountRate;
    }

    public Double getExpPerRes() {
        return expPerRes;
    }

    public void setExpPerRes(Double expPerRes) {
        this.expPerRes = expPerRes;
    }

    public Double getAvgExp_CustValue() {
        return avgExp_CustValue;
    }

    public void setAvgExp_CustValue(Double avgExp_CustValue) {
        this.avgExp_CustValue = avgExp_CustValue;
    }
    
    public Long getOutletId() {
        return (Long) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("outletId");
    }
    
    public String getStakeholderName(){
        return FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("stakeholderName").toString();
    }
}
